package de.koller.aframe.util.resource;

import de.koller.aframe.util.misc.ColorOperations;
import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.gl.util.RasterManager;
import de.koller.oceanFramework.util.MOut;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.imageio.ImageIO;

public class IOLoader {

	public static BufferedImage loadLocalImage( String file ) {
		try {
			return ImageIO.read( new File( file ) );
		} catch( IOException ex ) {
			throw new OceanException( ex );
		}
	}
	
	public static BufferedImage create( RasterManager raster ) {
		BufferedImage bi = new BufferedImage( raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_ARGB );
		for( int x = 0; x < raster.getWidth(); x++ )
			for( int y = 0; y < raster.getHeight(); y++ ) {
				bi.setRGB( x, y, raster.colorAt( x, y ) );
			}
		return bi;
	}
	
	public static RasterManager create( BufferedImage img, boolean withAlpha ) {
		RasterManager r = new RasterManager( img.getWidth(), img.getHeight() );
		for( int x = 0; x < r.getWidth(); x++ )
			for( int y = 0; y < r.getHeight(); y++ ) {
				int c = img.getRGB( x, y );
				if( !withAlpha )
					c = ColorOperations.setA( c, 0xff );
				r.setPixel( c, x, y );
			}
		return r;
	}
	
	public static OutputStream getCompressedOutputStream( String file ) {
		try {
			File dir = new File( file ).getParentFile();
			if( !dir.exists() )
				dir.mkdirs();
			
			OutputStream o = new BufferedOutputStream( new FileOutputStream( file ) );
			
			Deflater d = new Deflater( Deflater.BEST_COMPRESSION );
			return new DeflaterOutputStream( o, d );
		} catch ( IOException ex ) {
			throw new OceanException( ex );
		}
	}
	
	public static InputStream getCompressedInputStream( String file ) {
		try {
			InputStream i = new BufferedInputStream( new FileInputStream( file ) );
			
			Inflater d = new Inflater();
			return new InflaterInputStream( i, d );
		} catch ( IOException ex ) {
			throw new OceanException( ex );
		}
	}
	
	public static void saveGIF( BufferedImage img, String file ) {
		try {
			ImageIO.write( img, "gif", new File( file ) );
		} catch( IOException ex ) {
			System.err.println( ex );
		}
	}
	
	public static void savePNG( BufferedImage img, String file ) {
		try {
			ImageIO.write( img, "png", new File( file ) );
		} catch( IOException ex ) {
			System.err.println( ex );
		}
	}
	
	public static void main( String[] args ) {
		MOut o = new MOut( getCompressedOutputStream( "./screenshots/test.2m" ) );
		for( int i = 0; i < 10_000_000; i++ ) {
			o.writeInt( i );
			if( i % 100_000 == 0 )
				System.out.println( i );
		}
		o.close();
	}
	
}
